package com.acwer.oa.web.api.controller;

import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.acwer.dao.sys.SysRoleDao;
import com.acwer.dao.sys.SysUserDao;
import com.acwer.domain.sys.RpnMsg;
import com.acwer.domain.sys.SysUser;
import com.acwer.oa.web.SecurityUtil;
import com.acwer.oa.web.utils.JwtUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


@RestController
public class AuthController extends BaseController {

	@Autowired
	SecurityUtil securityUtil;
	@Autowired
	UserDetailsService userDetailsService;
	@Autowired
	SysUserDao userDao;
//	@Autowired
//	JwtUtil jwtUtil;
	@PostMapping("/login")
	public RpnMsg refreshAndGetAuthenticationToken(@RequestBody Map<String, String>form) {
		String username=form.get("username");
		String password=form.get("password");
		
		RpnMsg msg=SUCCESS();
		QueryWrapper<SysUser> userQuery=new QueryWrapper<SysUser>();
		userQuery.eq("account_id", username);
		userQuery.eq("password", password);
		SysUser user=userDao.selectOne(userQuery);
		if(user==null){
			msg=FAIL();
			msg.setMsg("用户名或密码错误！");
			return msg;
		}
		JSONObject o=new JSONObject();
		o.put("accessToken", JwtUtils.createJWT(UUID.randomUUID().toString(), username,user.getRole(), 7200000L));
		msg.setData(o);
		
		
		return msg;
	}
}
